package com.service.impl;

import com.constant.Constant;
import com.dao.CommentMapper;
import com.entity.Comment;
import com.entity.User;
import com.exception.ServiceException;
import com.github.pagehelper.PageHelper;
import com.service.CommentService;
import com.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageInfo;
import com.vo.CommentVo;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * date:2021/5/6
 * description:
 * Author:
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;


    //@李奇洲
    @Override
    public AjaxResult insertComment(HttpSession session, Integer resourceId, String context) {
        User user = (User) session.getAttribute(Constant.SESSION_USER);
        int status = Constant.COMMENT_STATUS_WAITING;
        Integer id = user.getId();
        Comment comment = new Comment();
        comment.setContext(context);
        comment.setUser_id(id);
        comment.setCreate_date(new Date());
        comment.setStatus(status);
        comment.setResource_id(resourceId);
        int i = commentMapper.insertComment(comment);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg(i == 1 ? "评论成功！进入待审核状态" : "插入失败！请检查Sql");
        return ajaxResult;
    }

    //获取全部评论  李奇洲
    @Override
    public AjaxResult findAllComment(Integer resourceId, Integer pageNo) {
        PageHelper.startPage(pageNo, Constant.PAGE_SIZE);
        List<Comment> list = commentMapper.selectAllComments(resourceId);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setObj(new PageInfo<Comment>(list));
        ajaxResult.setSuccess(true);
        if (null == list) {
            throw new ServiceException("没有更多评论了...");
        }
        return ajaxResult;
    }

    //吴彬彬     根据条件分页查询待审核评论
    @Override
    public PageInfo<List> findByChapterId(CommentVo commentVo) {
//        PageHelper.startPage(commentVo.getPageNo(), Constant.PAGE_SIZE);
        List<CommentVo> list = null;
        try {
            System.out.println("service" + commentVo.toString());
            list = commentMapper.findByChapterId(commentVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new PageInfo(list);
    }

    //吴彬彬  通过审核、禁用评论
    @Override
    public int updateStatus(Integer id, Integer status) {
        return commentMapper.updateStatus(id, status);
    }


    //吴彬彬   根据条件分页查询已审核评论(启用、禁用)
    @Override
    public PageInfo<List> findComs(CommentVo commentVo) {
        List<Comment> coms = commentMapper.findComs(commentVo);
        PageInfo info = new PageInfo(coms);
        return info;
    }

}